Option Strict On
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
'For the parameter-This declaration is needed, to access the ArrayList class
Imports System.Collections
Imports System.Web.UI.WebControls

Partial Class Default2
    Inherits System.Web.UI.Page
    Private RoutingOrderReport As ReportDocument

    Public Sub ConfigureCrystalReport(ByVal ReferenceNumber As Integer)
        Try
            RoutingOrderReport = New ReportDocument

            Dim strExportFile As String
            Dim objExOpt As ExportOptions
            Dim objDiskOpt As New DiskFileDestinationOptions

            strExportFile = Profile.BranchId.ToString + "_RoutingOrder.pdf"

            Dim reportPath As String = Server.MapPath("RoutingOrder.rpt")
            RoutingOrderReport.Load(reportPath)


            'Set Connection properties
            Dim connection As IConnectionInfo
            Dim serverName As String = ConfigurationManager.AppSettings("DatabaseName")

            For Each connection In RoutingOrderReport.DataSourceConnections
                ' Select Case connection.ServerName
                '  Case serverName
                connection.SetConnection(serverName, "GTSDB", True)
                'End Select
            Next

            Dim parameterField As New ParameterField
            parameterField = RoutingOrderReport.ParameterFields("ReferenceNumber")
            parameterField.CurrentValues.AddValue(ReferenceNumber)

            objDiskOpt.DiskFileName = Server.MapPath(".") & "/export/" & strExportFile
            objExOpt = RoutingOrderReport.ExportOptions
            objExOpt.ExportDestinationType = ExportDestinationType.DiskFile
            objExOpt.ExportFormatType = ExportFormatType.PortableDocFormat
            objExOpt.DestinationOptions = objDiskOpt
            'RoutingOrderReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "")
            RoutingOrderReport.Export()
            RoutingOrderReport.Close()
            Response.Redirect("export/" & strExportFile)
        Catch ex As Exception
            'lblMessage.Text = "System Error<br/>" + ex.Message + "<br/>" + ex.StackTrace
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim ReferenceNumber As Integer
            ReferenceNumber = Convert.ToInt32(Request.QueryString("ReferenceNumber"))
            ConfigureCrystalReport(ReferenceNumber)
        Catch ex As Exception
            lblMessage.Text = "System Error<br/>" + ex.GetHashCode.ToString
        End Try
    End Sub
End Class
